<template>
  <el-form :model="form" ref="form" :rules="rules" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input placeholder="用户名/手机" v-model="form.username"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password">
      <el-input placeholder="密码" type="password" v-model="form.password"></el-input>
    </el-form-item>

    <p class="form-text">
      <nuxt-link to="#">忘记密码</nuxt-link>
    </p>

    <el-button class="submit" type="primary" @click="handleLoginSubmit">登录</el-button>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      // 表单数据
      form: {
        username: "",
        password: ""
      },
      // 表单规则
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名或者手机号码",
            trigger: "blur"
          },
          {
            min: 5,
            max: 11,
            message: "请输入正确的用户名或者手机号码(长度在5到11位)",
            trigger: "change"
          }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 6,
            message: "请输入正确的密码(长度在3-6位)",
            trigger: "change"
          },
          {
            pattern: /^\d{3,6}$/,
            message: "长度在3-6位的数字",
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    // 提交登录
    handleLoginSubmit() {
      this.$refs.form.validate((valid, ojbValue) => {
        if (valid) {
          this.$store.dispatch("user/setuserInof", this.form).then(res => {
            console.log(res.data);
            // //登录成功后的处理
            this.form.username = "";
            this.form.password = "";
            this.$notify({
              title: "尊敬的超级会员",
              message: "欢迎登录",
              type: "success"
            });
            this.$router.push("/");
          });
        }
      });
    }
  }
};
</script>

<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>